﻿@model Wicresoft.UnifyShow.Portal2.ViewModels.PagedUser_XsModel
@{
    ViewBag.Title = "配置用户";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
@section ScriptAndCss{
    <script type="text/javascript">


    var gridPage=new Object();
    gridPage.currentpage=1;

        function initgridUser_X_Index() {
            $("tr", $("thead", "#gridUser_X_Index")).children().first().hide();
//            $("tr", $("tbody", "#gridUser_X_Index")).click(function () {
//                $(this).siblings().css("background-color", "white");
//                $(this).css("background-color", "#ccc");
//                var id = $(this).children().first().text();
//                var login = $($(this).children()[2]).text();
//                var url = "@Url.Action("UserInfo", "AdminPermission")";
//                var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
//                url += "?userID=" + id + "&nocatch=" + rnd;
//                $("#UserID").val(id);
//                $("#login").val(login);
//                $("#userInfo").load(url);
//                $("#userInfo").attr("data-url", url);

//            });
        }


        function check() {
            var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
            var displayname = $("input#Check_UserDisplayName").val();
            var url = "/AdminPermission/Index?name=" + $("input#Check_UserName").val() + "&&displayname=" + encodeURI(displayname) + "&nocatch=" + rnd;
            $("#gridUser_X_Index").load(url + ' #gridUser_X_Index', initgridUser_X_Index);
        }
        function editUserDialog() {
            var url = "@Url.Action("UserEdit", "AdminPermission")";
            url += "?userID=" + $("#UserID").val();
            $("#userEdit").dialog({
                href: url
            });
            $("#userEdit").dialog("open");
        }

        function editPassWordDialog() {
            var url = "@Url.Action("ChangePassword", "AdminPermission")";
            $("#userChangePassWord").dialog({
                href: url
            });
            $("#userChangePassWord").dialog("open", false);
        }
        $(function () {
            $("tr", $("thead", "#gridUser_X_Index")).children().first().hide();
            $("#gridUser_X_Index").on("click","tbody tr",function () {
                $(this).siblings().css("background-color", "white");
                $(this).css("background-color", "#ccc");
                var id = $(this).children().first().text();
                var login = $($(this).children()[2]).text();
                var url = "@Url.Action("UserInfo", "AdminPermission")";
                var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
                url += "?userID=" + id + "&nocatch=" + rnd;
                $("#UserID").val(id);
                $("#login").val(login);
                $("#userInfo").load(url);
                $("#userInfo").attr("data-url", url);
            });

            $("#gridUser_X_Index").on("click","tfoot a",function () {
            //alert($(this).text());
            gridPage.currentpage=parseInt($(this).text());
            });

            $("#userEdit").dialog({
                closed: true,
                width: 560,
                height: 320,
                modal: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        var email = $("#Email").val();
                        var reg = /^[\w-]+(\.[\w-]+)*@@([\w-]+\.)+[a-zA-Z]+$/;
                        var mobile = $("#Mobile").val();
                        var reg1 = /(^(\d{3,4}-)?\d{7,8})$/;
                        var reg2 = /^((\+86)|(86))?((13)\d{9}$)|((14)\d{9}$)|((15)\d{9}$)|((18)\d{9}$)/;
                        var position = $("#Position").val();

                        //var flag = $("#Disabled").attr("checked");
                        //alert(flag);

                        if ($("#Disabled").attr("checked") == "checked") {
                            //alert("ture");
                            $.post("@Url.Action("UserEdit", "AdminPermission")",
                            $("#userEditorForm").serialize(),
                            function (data) {
                                if (data != "") {
                                    alert(data);
                                }
                                else {
                                    $("#userEdit").dialog("close");
                                    var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
                                    $("#userInfo").load($("#userInfo").attr("data-url") + "&nocatch=" + rnd);
                                    var displayname = $("input#Check_UserDisplayName").val();
                                    var urls = "/AdminPermission/Index?name=" + $("input#Check_UserName").val() + "&&displayname=" + encodeURI(displayname) + "&nocatch=" + rnd+ "&page=" + gridPage.currentpage;
                                    $("#gridUser_X_Index").load(urls + ' #gridUser_X_Index', initgridUser_X_Index);
                                    alert("用户信息修改成功！");
                                    //window.location.reload();
                                }
                            });

                        }else {

                            if (email == "") {
                                alert("必填项，请输入Email");
                                return false;
                            }
                            //else if (position == "") {
                            //    alert("必填项，请输入职务");
                            //    return false;
                            //}
                            else if (reg.test(email)) {
                                if (mobile == "") {
                                    alert("必填项，请输入联系方式");
                                    return false;
                                } else if (reg1.test(mobile) || reg2.test(mobile)) {
                                    $.post("@Url.Action("UserEdit", "AdminPermission")",
                                    $("#userEditorForm").serialize(),
                                    function (data) {
                                        if (data != "") {
                                            alert(data);
                                        }
                                        else {
                                            $("#userEdit").dialog("close");
                                            var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
                                            $("#userInfo").load($("#userInfo").attr("data-url") + "&nocatch=" + rnd);
                                            var displayname = $("input#Check_UserDisplayName").val();
                                            var urls = "/AdminPermission/Index?name=" + $("input#Check_UserName").val() + "&&displayname=" + encodeURI(displayname) + "&nocatch=" + rnd+ "&page=" + gridPage.currentpage;
                                            $("#gridUser_X_Index").load(urls + ' #gridUser_X_Index', initgridUser_X_Index);                                       
                                            alert("用户信息修改成功！");
                                            //("#gridUser_X_Index").load(urls + ' #gridUser_X_Index', initgridUser_X_Index);
                                            //window.location.reload();
                                        }
                                    });
                                }
                                else {
                                    alert("信息输入有误，请核对后再提交。");
                                    return false;
                                }
                            }

                        }
                    }
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () { $('#userEdit').dialog("close"); }
                }]
            });
            $("#userChangePassWord").dialog({
                closed: true, width: 560, height: 150, modal: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        if ($("#mpassword").val() == $("#cmpassword").val()) {
                            $.post("@Url.Action("ChangePassword", "AdminPermission")",
                            "login=" + $("#login").val() + "&pass=" + $("#mpassword").val(),
                            function () {
                                alert("密码修改成功！");
                                $("#userChangePassWord").dialog("close");
                            });
                        }
                        else {
                            alert("两次密码输入不一致，请重新输入");
                        }
                    }
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () { $("#userChangePassWord").dialog("close"); }
                }]
            });
        });
    </script>
    <style type="text/css">
        .main_left .left_menu
        {
            border: 1px solid #92a9bb;
            height: 600px;
        }
    </style>
}
<div class="index_lr">
    <ul>
        <li class="index_lr2_M">
            <div class="index_lrA_title ui-icons">
                <h3>
                    用户列表</h3>
            </div>
            <div class="index_pt_bg">
                <div class="search_bg">
                    <label>
                        登录名：</label>
                    <input id="Check_UserName" type="text" value="@ViewBag.name" />
                    <label>
                        姓名：</label>
                    <input id="Check_UserDisplayName" type="text" value="@ViewBag.displayname" />
                    <input type="button" id="check" value="查询" onclick="check();" />
                </div>
                <br />
                <div id="gridUser_X_Index">
                    @{
                        var grid = new WebGrid(null, rowsPerPage: Model.PageSize,
                             ajaxUpdateContainerId: "gridUser_X_Index", canPage: true, canSort: true, ajaxUpdateCallback: "initgridUser_X_Index");
                        grid.Bind(Model.User_Xs, new[] { "ID", "DisplayName", "Name", "Company", "Department", "Position", "IsAdmin" }, autoSortAndPage: false, rowCount: int.Parse(Model.TotalRows + ""));
                    }
                    @grid.GetHtml(tableStyle: "ContactTB talbe_gray", headerStyle: "ContactTH", htmlAttributes: new { width = "100%", border = 0, cellpadding = 0, cellspacing = 0 },
                    columns: grid.Columns(grid.Column("ID", "标识符", style: "Hiden"),
                    grid.Column("DisplayName", "姓名", style: "ContactTD"),
                    grid.Column("Name", "登录名", style: "ContactTD"),
                    grid.Column("Company", "公司", style: "ContactTD"),
                    grid.Column("Department", "部门", style: "ContactTD"),
                    grid.Column("Position", "职务", style: "ContactTD"),
                    grid.Column("IsAdmin", "配置管理员", style: "ContactTD", format: @<text><input type="checkbox" disabled="disabled" @if ((item.IsAdmin ?? 0) != 0)
                                                                                                                                  { <text> checked="checked" </text> }/></text>)))
                </div>
            </div>
        </li>
    </ul>
</div>
<input type="hidden" id="UserID" />
<input type="hidden" id="login" />
<div class="clear">
</div>
<div id="userInfo" data-url="">
</div>
<div id="userEdit" class="easyui-dialog" title="编辑用户权限">
</div>
<div id="userChangePassWord" title="更改用户密码">
</div>
